//
//  CZWelcomeViewController.swift
//  git
//
//  Created by 徐 鹏超 on 16/4/10.
//  Copyright © 2016年 徐 鹏飞. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class CZWelcomeViewController: UIViewController {
    /*
    实现步骤:
    1. 懒加载控件
    2. 给控件设置约束
    3. 实现视图功能
    */
    
    override func loadView() {
        view = bigImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        if let urlString = CZUserModel.getModel()?.avatar_large{
            let url = NSURL(string: urlString)
            photoImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    
    private func setupUI(){
        view.addSubview(photoImageView)
        view.addSubview(messageLabel)
        
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(photoImageView.snp_bottom).offset(10)
        }
        
    }
    //视图已经显示了
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //动画在视图显示完成之后调用执行
        startAnimation()
    }
    
    private func startAnimation(){
        //更新一下头像的约束
        self.photoImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 200)
        }
        //弹簧动画效果
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7,initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
            //实现页面跳转
            NSNotificationCenter.defaultCenter().postNotificationName(kNotificationChangeController, object: self)
        }
        
        
    }
    
    
    
    //懒加载
    //背景图片
    private lazy var bigImageView: UIImageView = {
        let bigImageView = UIImageView()
        
        bigImageView.image = UIImage(named: "ad_background")
        
        return bigImageView
    }()
    //头像
    private lazy var photoImageView: UIImageView = {
        let photoImageView = UIImageView()
        
        photoImageView.image = UIImage(named: "avatar_default_big")
        
        // 修改layer
        photoImageView.layer.cornerRadius = 170/4
        
        photoImageView.layer.borderWidth = 2
        
        photoImageView.layer.borderColor = UIColor.grayColor().CGColor
        // 切除多余的视图
        photoImageView.layer.masksToBounds = true
        
        return photoImageView

    }()
    
    /// 欢迎语
    private lazy var messageLabel: UILabel = {
        let messageLabel = UILabel()
        
        // 先得获取名字
        // ?? 含义是 如果?? 前边的可选项有值就用解包后的值,
        //如果没有值,就用?? 后边的值
        messageLabel.text = "\(CZUserModel.getModel()?.name ?? "")大圣归来"
        
        messageLabel.textAlignment = .Center
        
        messageLabel.textColor = UIColor.darkGrayColor()
        
        messageLabel.font =  UIFont.systemFontOfSize(16)
        
        return messageLabel
    }()
    
    
    
    
    
    
}
